package com.sanyou.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sanyou.mapper.DataInfoDetailMapper;
import com.sanyou.mapper.DataInfoMapper;
import com.sanyou.pojo.DataInfo;
import com.sanyou.pojo.DataInfoDetail;
import com.sanyou.pojo.vo.SimpleDataInfoVo;
import com.sanyou.service.DataInfoService;
import com.sanyou.utils.PagedResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Service
public class DataInfoServiceImpl implements DataInfoService {
    @Autowired
    DataInfoMapper dataInfoMapper;

    @Autowired
    DataInfoDetailMapper dataInfoDetailMapper;

    @Override
    public List<DataInfo> getAllDataInfo() {
        return dataInfoMapper.getAllDataInfo();
    }

    @Override
    public PagedResult getAllDataInfo(Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<DataInfo> list = this.getAllDataInfo();
        PageInfo<DataInfo> pageList = new PageInfo<>(list);

        //分页
        PagedResult pagedResult = new PagedResult();
        pagedResult.setPage(page);
        pagedResult.setTotal(pageList.getPages());
        pagedResult.setRows(list);
        pagedResult.setRecords(pageList.getTotal());
        return pagedResult;
    }

    @Override
    public List<DataInfoDetail> getDataInfoDetailsById(String parentId) {
        return this.getDataInfoDetailsById(parentId,"");
    }

    @Override
    public List<DataInfoDetail> getDataInfoDetailsByDataCode(String dataCode) {
        return this.getDataInfoDetailsByDataCode(dataCode,"");
    }

    @Override
    public List<DataInfoDetail> getDataInfoDetailsById(String parentId, String enableMark) {
        return dataInfoMapper.getDataInfoDetailsById(parentId,enableMark);
    }

    @Override
    public List<DataInfoDetail> getDataInfoDetailsByDataCode(String dataCode, String enableMark) {
        return dataInfoMapper.getDataInfoDetailsByDataCode(dataCode,enableMark);
    }

    @Override
    public List<SimpleDataInfoVo> getSimpleDataInfoDetailsByDataCode(String dataCode) {
        return dataInfoMapper.getSimpleDataInfoDetailsByDataCode(dataCode,"1");
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void upsertDataInfo(DataInfo dataInfo) {
        DataInfo temp = this.dataInfoMapper.getDataInfoByDataCode(dataInfo.getDataCode());
        if(dataInfo.getId()!=null){
            //合法性判断，确保字典编号不重复
            if(temp!=null && !(dataInfo.getId().equals(temp.getId()))){
                throw new RuntimeException("当前字典编号已存在!");
            }
            dataInfo.setUpdatetime(new Date());//更新更新时间
            //更新数据字典信息
            Example example = new Example(DataInfo.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("id",dataInfo.getId());
            dataInfoMapper.updateByExampleSelective(dataInfo,example);
        }else {
            //合法性判断，确保字典编号不重复
            if(temp!=null){
                return;
            }

            //初始化数据
            dataInfo.setCreatetime(new Date());//初始化创建时间
            dataInfo.setUpdatetime(new Date());//初始化更新时间
            dataInfo.setDeleteMark((byte)0);//初始化删除标记为未删除状态
            //新增数据字典信息
            dataInfoMapper.insertUseGeneratedKeys(dataInfo);
        }
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void upsertDataInfoDetail(DataInfoDetail dataInfoDetail) {
        List<DataInfoDetail> details = this.getDataInfoDetailsByDataCode(dataInfoDetail.getParentCode());
        if(dataInfoDetail.getId()!=null){
            //合法性判断，确保映射值不重复
            for (DataInfoDetail detail:details) {
                if(detail.getId()!=dataInfoDetail.getId()
                        && detail.getDataCode().equals(dataInfoDetail.getDataCode())){
                    throw new RuntimeException("当前映射值已存在!");
                }
            }

            //初始化数据
            dataInfoDetail.setUpdatetime(new Date());//初始化更新时间

            //更新数据字典字段映射信息
            Example example = new Example(DataInfo.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("id",dataInfoDetail.getId());
            dataInfoDetailMapper.updateByExampleSelective(dataInfoDetail,example);
        }else {
            //合法性判断，确保映射值不重复
            for (DataInfoDetail detail:details) {
                if(detail.getDataCode().equals(dataInfoDetail.getDataCode())){
                    throw new RuntimeException("当前映射值已存在!");
                }
            }

            //初始化数据
            if(dataInfoDetail.getEnableMark()==null)dataInfoDetail.setEnableMark((byte)0);
            dataInfoDetail.setCreatetime(new Date());//初始化创建时间
            dataInfoDetail.setUpdatetime(new Date());//初始化更新时间
            dataInfoDetail.setDeleteMark((byte)0);//初始化删除标记为未删除状态
            //新增数据字典字段映射信息
            dataInfoDetailMapper.insertUseGeneratedKeys(dataInfoDetail);
        }
    }

    @Override
    public void disableDataInfoDetailById(String id) {
        if(StringUtils.isBlank(id)){return;}

        //获取目标字段映射信息并更新启用字段
        DataInfoDetail dataInfoDetail = this.getDataInfoDetailById(id);
        dataInfoDetail.setEnableMark((byte) 0);
        this.upsertDataInfoDetail(dataInfoDetail);
    }

    @Override
    public void enableDataInfoDetailById(String id) {
        if(StringUtils.isBlank(id)){return;}

        //获取目标字段映射信息并更新启用字段
        DataInfoDetail dataInfoDetail = this.getDataInfoDetailById(id);
        dataInfoDetail.setEnableMark((byte) 1);
        this.upsertDataInfoDetail(dataInfoDetail);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void deleteDataInfoById(String id) {
        if(StringUtils.isBlank(id)){return;}

        //获取该字典下所有字段映射并删除
        List<DataInfoDetail> details = this.getDataInfoDetailsById(id);
        for (DataInfoDetail detail:details) {
            this.deleteDataInfoDetailById(detail.getId().toString());
        }
        //删除字典
        dataInfoMapper.deleteByPrimaryKey(Integer.parseInt(id));
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void deleteDataInfoByCode(String dataCode) {
        if(StringUtils.isBlank(dataCode)){return;}

        //获取该字典下所有字段映射并删除
        List<DataInfoDetail> details = this.getDataInfoDetailsByDataCode(dataCode);
        for (DataInfoDetail detail:details) {
            dataInfoDetailMapper.deleteByPrimaryKey(detail.getId());
        }
        //删除字典
        Example example = new Example(DataInfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("dataCode",dataCode);
        dataInfoMapper.deleteByExample(example);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void deleteDataInfoDetailById(String id) {
        if(StringUtils.isBlank(id)){return;}
        dataInfoDetailMapper.deleteByPrimaryKey(Integer.parseInt(id));
    }

    @Override
    public DataInfo getDataInfoById(String id) {
        return dataInfoMapper.selectByPrimaryKey(Integer.parseInt(id));
    }

    @Override
    public DataInfo getDataInfoByCode(String dataCode) {
        Example example = new Example(DataInfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("dataCode",dataCode);
        return dataInfoMapper.selectOneByExample(example);
    }

    @Override
    public DataInfoDetail getDataInfoDetailById(String id) {
        return dataInfoDetailMapper.selectByPrimaryKey(Integer.parseInt(id));
    }

    @Override
    public DataInfoDetail getDataInfoDetailByParentCodeAndDataCode(String parentCode, String dataCode) {
        Example example = new Example(DataInfoDetail.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("parentCode",parentCode);
        criteria.andEqualTo("dataCode",dataCode);
        return dataInfoDetailMapper.selectOneByExample(example);
    }
}
